<?php

namespace App\Admin\Controllers\Admin;

use App\Admin\Actions\Admin\BatchAddDomain;
use App\Models\DomainList;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class DomainListController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '域名';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new DomainList());
        $grid->model()->orderBy('sort');

        $grid->column('id', __('Id'));
        $grid->column('domain', __('域名'));

        $grid->column('state', __('状态'))->display(function($state){

            if($state){
                return "<span style='color:red' >被封</span>";
            }else{
                return "<span style='color:green' >正常</span>";
            }
        });
        $grid->column('use_num', __('使用次数'));
        $grid->column('sort', __('排序'))->editable();
        $grid->column('created_at', __('添加时间'));
        $grid->column('updated_at', __('使用时间'));
        $grid->actions(function($action){
            $action->disableView();
            $action->disableEdit();
        });

        $grid->tools(function (Grid\Tools $tools) {
            // excle 导入
            $tools->append(new BatchAddDomain());
        });

        $grid->quickSearch('domain');

        $grid->filter(function ($filter) {

            // 范围过滤器，调用模型的`onlyTrashed`方法，查询出被软删除的数据。
            $filter->scope('trashed', '回收站')->onlyTrashed();

            $filter->disableIdFilter();
            $filter->equal('state', '域名状态')->select([
                0 => '正常',
                1 => '被封',
            ]);
        });

        return $grid;
    }

    public function form(){
        $form = new Form(new DomainList());
        $form->text('domain', __('域名'));
        $form->number('sort', __('排序'));
        return $form;
    }

}
